/**
 * 初始化页面
 */
$(function () {
    var id = sessionStorage.getItem("id");
    // console.log("id="+id);
    if (id == null) {
        return false;
    }
    $.ajax({
        type: 'get',
        url: "http://api.hk.com/nav/findById/"+id,
        success: function (data) {
            if (data.message == "success") {
                let {id,name,url} = data.object;
                $("#L_id").val(id);
                $("#L_name").val(name);
                $("#L_url").val(url);
                return false;
            }
            layer.msg('信息获取失败，请稍后重试！',{icon:0,time:1200});
            return false;
        },
        error: function () {
            layer.msg('请求接口异常！', {icon: 0, time: 1200});
            return false;
        }
    })
});



layui.use(['form', 'layer', 'jquery'], function () {
    $ = layui.jquery;
    var form = layui.form;
    var layer = layui.layer;

    //自定义验证规则
    form.verify({
        L_url: [/(\/.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/, "请输入正确路径！"],
    })

    //监听提交
    form.on('submit(add)', function (data) {
        // console.log(data.field);
        //发异步，把数据提交给Java
        $.ajax({
            type: 'post',
            url: 'http://api.hk.com/nav/updateMessage',
            data: JSON.stringify(data.field),
            contentType: 'application/json;charset=utf-8',
            dataType: 'json',
            success: function (res) {
                if (res.message == "success") {
                    $("#sumbit").attr("disabled",true); //禁止点击按钮
                    layer.msg('更新成功！', {icon: 1, time: 1100,end:function () {
                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        }});
                    return false;
                }
                layer.msg('更新失败！', {icon: 2, time: 1200});
                return false;
            },
            error: function () {
                layer.msg('请求接口异常！', {icon: 0, time: 1200});
                return false;
            }
        });
        return false;
    });

});

/**
 * 导航修改表单信息重置
 */
function resetNavForm() {

    var id = sessionStorage.getItem("id");
    // console.log("id="+id);
    if (id == null) {
        return false;
    }
    $.ajax({
        type: 'get',
        url: "http://api.hk.com/nav/findById/"+id,
        success: function (data) {
            if (data.message == "success") {
                let {id,name,url} = data.object;
                $("#L_id").val(id);
                $("#L_name").val(name);
                $("#L_url").val(url);
                return false;
            }
            layer.msg('重置失败，请稍后重试！',{icon:0,time:1200});
            return false;
        },
        error: function () {
            layer.msg('请求接口异常！', {icon: 0, time: 1200});
            return false;
        }
    })

}

// var _hmt = _hmt || [];
// (function () {
//     var hm = document.createElement("script");
//     hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
//     var s = document.getElementsByTagName("script")[0];
//     s.parentNode.insertBefore(hm, s);
// })();